package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.BrewView;

public class BrewViewPresenter extends AbstractPresenter<BrewView> {
	final String SINGLEITEM = "brewView";
	final String MULTIITEM = "brewViews";
	
	public ArrayList<BrewView> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public BrewView tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}

	public BrewView createItemFromJSONObject(JSONObject jsonObject){
		BrewView item = new BrewView();
		try{
			item.setBrewID(jsonObject.getInt("bid"));
			item.setBrewNoteID1(jsonObject.getInt("brewNoteID"));
			item.setBrewNoteID2(jsonObject.getInt("brewNoteID2"));
			item.setBrewNoteID3(jsonObject.getInt("brewNoteID3"));
			item.setBrewNoteID4(jsonObject.getInt("brewNoteID4"));
			item.setBrewNoteID5(jsonObject.getInt("brewNoteID5"));
			item.setBrewNoteID6(jsonObject.getInt("brewNoteID6"));
			item.setConditioningVesselID(jsonObject.getInt("cvid"));
			item.setGanttColor(jsonObject.getInt("ganttColor"));
			item.setGanttConditioningID(jsonObject.getInt("ganttCID"));
			item.setGanttFGICSV(jsonObject.getString("ganttFGI"));
			item.setGanttFilter(jsonObject.getBoolean("ganttFilter"));
			item.setGanttFine(jsonObject.getBoolean("ganttFine"));
			item.setGanttHarvest(jsonObject.getBoolean("ganttHarvest"));
			item.setGanttPrimaryID(jsonObject.getInt("ganttPID"));
			item.setGanttSecondaryID(jsonObject.getInt("ganttSID"));
			item.setOrigBrew(jsonObject.getInt("origBrew"));
			item.setPrimaryVesselID(jsonObject.getInt("pfvid"));
			item.setSecondaryVesselID(jsonObject.getInt("sfvid"));
			item.setState(jsonObject.getInt("bState"));
			item.setVersion(jsonObject.getInt("version"));
			item.setVolumeLeft(jsonObject.getDouble("volLeft"));
			
			if(!jsonObject.isNull("bDate")){
				item.setBrewDate(timeFormat.parse(jsonObject.getString("bDate")));
			} else {
				item.setBrewDate(null);
			}
			if(!jsonObject.isNull("ganttStart")){
				item.setGanttStart(timeFormat.parse(jsonObject.getString("ganttStart")));
			} else {
				item.setGanttStart(null);
			}
			if(!jsonObject.isNull("ganttCond")){
				item.setGanttCond(timeFormat.parse(jsonObject.getString("ganttCond")));
			} else {
				item.setGanttCond(null);
			}
			if(!jsonObject.isNull("ganttSecond")){
				item.setGanttSecond(timeFormat.parse(jsonObject.getString("ganttSecond")));
			} else {
				item.setGanttSecond(null);
			}
			if(!jsonObject.isNull("ganttStop")){
				item.setGanttStop(timeFormat.parse(jsonObject.getString("ganttStop")));
			} else {
				item.setGanttStop(null);
			}
			if(!jsonObject.isNull("ganttHarvestDate")){
				item.setGanttHarvestDate(timeFormat.parse(jsonObject.getString("ganttHarvestDate")));
			} else {
				item.setGanttHarvestDate(null);
			}
			if(!jsonObject.isNull("cTDate")){
				item.setConditioningDate(timeFormat.parse(jsonObject.getString("cTDate")));
			} else {
				item.setConditioningDate(null);
			}
			if(!jsonObject.isNull("sFDate01")){
				item.setSecondaryFermDate(timeFormat.parse(jsonObject.getString("sFDate01")));
			} else {
				item.setSecondaryFermDate(null);
			}
			if(!jsonObject.isNull("pDate")){
				item.setPackagingDate(timeFormat.parse(jsonObject.getString("pDate")));
			} else {
				item.setPackagingDate(null);
			}
			
			POSBeerViewPresenter presenter = new POSBeerViewPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("bBeerID");
			item.setBeer(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(BrewView item){
		try{
			JSONObject json = new JSONObject();
			json.put("bid", item.getBrewID());
			json.put("brewNoteID", item.getBrewNoteID1());
			json.put("brewNoteID2", item.getBrewNoteID2());
			json.put("brewNoteID3", item.getBrewNoteID3());
			json.put("brewNoteID4", item.getBrewNoteID4());
			json.put("brewNoteID5", item.getBrewNoteID5());
			json.put("brewNoteID6", item.getBrewNoteID6());
			json.put("cvid", item.getConditioningVesselID());
			json.put("ganttCID", item.getGanttConditioningID());
			json.put("ganttColor", item.getGanttColor());
			json.put("ganttFGI", item.getGanttFGICSV());
			json.put("ganttFilter", item.isGanttFilter());
			json.put("ganttFine", item.isGanttFine());
			json.put("ganttHarvest", item.isGanttHarvest());
			json.put("ganttPID", item.getGanttPrimaryID());
			json.put("ganttSID", item.getGanttSecondaryID());
			json.put("origBrew", item.getOrigBrew());
			json.put("pfvid", item.getPrimaryVesselID());
			json.put("sfvid", item.getSecondaryVesselID());
			json.put("version", item.getVersion());
			json.put("volLeft", item.getVolumeLeft());
			json.put("bState", item.getState());
			
			if(item.getGanttSecond()!=null){
				json.put("ganttSecond", getDate(item.getGanttSecond()));
			} else {
				json.put("ganttSecond", JSONObject.NULL);
			}
			if(item.getGanttStart()!=null){
				json.put("ganttStart", getDate(item.getGanttStart()));
			} else {
				json.put("ganttStart", JSONObject.NULL);
			}
			if(item.getGanttStop()!=null){
				json.put("ganttStop", getDate(item.getGanttStop()));
			} else {
				json.put("ganttStop", JSONObject.NULL);
			}
			if(item.getGanttHarvestDate()!=null){
				json.put("ganttHarvestDate", getDate(item.getGanttHarvestDate()));
			} else {
				json.put("ganttHarvestDate", JSONObject.NULL);
			}
			if(item.getConditioningDate()!=null){
				json.put("cTDate", getDate(item.getConditioningDate()));
			} else {
				json.put("cTDate", JSONObject.NULL);
			}
			if(item.getSecondaryFermDate()!=null){
				json.put("sFDate01", getDate(item.getSecondaryFermDate()));
			} else {
				json.put("sFDate01", JSONObject.NULL);
			}
			if(item.getPackagingDate()!=null){
				json.put("pDate", getDate(item.getPackagingDate()));
			} else {
				json.put("pDate", JSONObject.NULL);
			}
			if(item.getBrewDate()!=null){
				json.put("bDate", getDate(item.getBrewDate()));
			} else {
				json.put("bDate", JSONObject.NULL);
			}
			if(item.getGanttCond()!=null){
				json.put("ganttCond", getDate(item.getGanttCond()));
			} else {
				json.put("ganttCond", JSONObject.NULL);
			}

			POSBeerViewPresenter presenter = new POSBeerViewPresenter();
			json.put("bBeerID", presenter.createJSON(item.getBeer()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public BrewView getEmptyEntity(){
		BrewView entity = new BrewView();
		return entity;
	}
}
